package com.deyuanyun.pic.settings.service.greatwall.impl;

import com.deyuanyun.pic.settings.domain.greatwall.LogUser;
import com.deyuanyun.pic.settings.domain.greatwall.LogUserExample;
import com.deyuanyun.pic.settings.domain.greatwall.LogUserExpand;
import com.deyuanyun.pic.settings.mapper.greatwall.LogUserExpandMapper;
import com.deyuanyun.pic.settings.mapper.greatwall.LogUserMapper;
import com.deyuanyun.pic.settings.service.greatwall.LogUserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.joda.time.LocalDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 *  产品日志用户信息service实现类
 *  @author axiao(xj08.dream@gmail.com)
 *  @date 2016-12-20
 *  @version 1.0
 *  Histroy: [修改人][时间][描述]
 */
@Service
public class LogUserServiceImpl implements LogUserService {

    @Autowired
    private LogUserMapper logUserMapper;

    @Autowired
    private LogUserExpandMapper logUserExpandMapper;

    @Override
    public int insertSelective(LogUser record) {
        return logUserMapper.insertSelective(record);
    }

    @Override
    public int updateByPrimaryKeySelective(LogUser record) {
        return logUserMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public LogUser selectByCodeAndName(String code, String userName) {
        LocalDate localDate = LocalDate.now();
        LogUserExample logUserExample = new LogUserExample();
        logUserExample.setOrderByClause("create_date desc");
        logUserExample.createCriteria().andDeletedEqualTo((short) 0).andCodeEqualTo(code).andUserNameEqualTo(userName)
                .andCreateDateGreaterThanOrEqualTo(localDate.toDate()).andCreateDateLessThan(localDate.plusDays(1).toDate());
        List<LogUser> logUsers = logUserMapper.selectByExample(logUserExample);
        if(logUsers != null && !logUsers.isEmpty()) {
            return logUsers.get(0);
        }
        return null;
    }

    @Override
    public Page<LogUserExpand> queryUntreatedLogs(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return logUserExpandMapper.queryUntreatedLogs(new Date());
    }
}
